#!/usr/bin/env python

from __future__ import print_function, division, unicode_literals

__doc__ = """filter txinfo files by given criteria"""

import argparse
import os
import sys

import uta.formats.exonset as ufes


def parse_args(argv):
    ap = argparse.ArgumentParser(
        description=__doc__,
    )
    ap.add_argument(
        "FILE"
    )
    ap.add_argument(
        "--transcripts-from", "-T",
    )

    opts = ap.parse_args(argv)
    return opts


if __name__ == "__main__":
    opts = parse_args(sys.argv[1:])

    filter_acs = set()
    if opts.transcripts_from:
        filter_acs = set([l.strip()
                          for l in open(opts.transcripts_from, "r").readlines()])

    esr = ufes.ExonSetReader(
        sys.stdin if opts.FILE == "-" else open(opts.FILE, "r"))
    esw = ufes.ExonSetWriter(sys.stdout)

    for es in esr:
        if es.tx_ac in filter_acs:
            esw.write(es)
